﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication3
{
    public partial class FormStuInfo : FormBase
    {
        public FormStuInfo()
        {
            InitializeComponent();
        }

        public FormStuInfo(ToolStripMenuItem tsMi)
            : base(tsMi)
        {
            InitializeComponent();
            WindowState = FormWindowState.Maximized;
        }

        private void FormStuInfo_Load(object sender, EventArgs e)
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = "Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=master";
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select * from student;";

            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = cmd;

            DataTable dt = new DataTable();
            adapter.Fill(dt);
            //NewRow后行状态属于Detached
            //DataRow dr = dt.NewRow();
            //Add后，行状态变为Added
            //dt.Rows.Add(dr);

            BindingSource bs = new BindingSource();
            DataView dv = dt.DefaultView;
            bs.DataSource = dv;
            dataGridView1.DataSource = bs;
            bindingNavigator1.BindingSource = bs;
        }

        DataTable GetChangesByGrid(DataGridView dgv)
        {
            DataTable dt = null;
            if (dgv.DataSource is BindingSource)
            {
                BindingSource bs = dataGridView1.DataSource as BindingSource;
                if (bs.DataSource is DataView)
                {
                    DataView dv = bs.DataSource as DataView;
                    dt = dv.Table.GetChanges();
                }
            }
            return dt;
        }

        DataTable GetChangesByGrid(DataGridView dgv, DataRowState drs)
        {
            DataTable dt = null;
            if (dgv.DataSource is BindingSource)
            {
                BindingSource bs = dataGridView1.DataSource as BindingSource;
                if (bs.DataSource is DataView)
                {
                    DataView dv = bs.DataSource as DataView;
                    dt = dv.Table.GetChanges(drs);
                }
            }
            return dt;
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataTable dtChanges = GetChangesByGrid(dataGridView1);
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = new OleDbConnection();
            try
            {
                conn.ConnectionString = "Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=master";
                conn.Open();
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = conn;
                cmd.CommandText = "delete from student where sno = ?";
                //处理删除
                DataTable dtDeleted = GetChangesByGrid(dataGridView1, DataRowState.Deleted);
                foreach (DataRow dr in dtDeleted.Rows)
                {
                    cmd.Parameters.Clear();
                    //对于删除行，只能访问Original
                    OleDbParameter param = new OleDbParameter();
                    param.Value = dr["sno", DataRowVersion.Original];
                    cmd.Parameters.Add(param);
                    int rowAffected = cmd.ExecuteNonQuery();
                }
                //处理更新数据


                //处理插入数据

                MessageBox.Show("恭喜您，数据保存成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            //dtChanges = GetChangesByGrid(dataGridView1, DataRowState.Modified);
            //dtChanges = GetChangesByGrid(dataGridView1, DataRowState.Added);

            //DataView dv = new DataView(dtChanges, null, null, DataViewRowState.Deleted);
            //dv = new DataView(dtChanges, null, null, DataViewRowState.ModifiedCurrent);
            //dv = new DataView(dtChanges, null, null, DataViewRowState.Added);
        }
    }
}
